package com.mingqijia.gassafety.shared.utils.message;

import cn.hutool.core.date.DateUtil;
import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.mingqijia.gassafety.shared.constant.Constants;
import com.mingqijia.gassafety.shared.dto.AppletInfoReqDTO;
import com.mingqijia.gassafety.shared.dto.cms.CmsInfoReqDTO;
import com.mingqijia.gassafety.shared.dto.cms.CmsInfoResDTO;
import com.mingqijia.gassafety.shared.utils.HttpUtil;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

/**
 * @version 1.0.0
 * @ClassName AppletMessageService.java
 * @createTime 2024年04月29日
 */
@Component
@Slf4j
public class AppletMessageService {
    @Value(value = "${asst.service.endpoint}")
    public String endpoint;

    @Value(value = "${asst.service.templateId}")
    public String templateId;

    @Value(value = "${asst.service.runningTemplateId}")
    public String runningTemplateId;

    /**
     * 微信小程序发送
     *
     * @param appletReqParams
     * @return
     */
    public CmsInfoResDTO sendAppletMsg(JSONObject appletReqParams) {
        log.info("url:"+endpoint);
        String result = HttpUtil.doPostMini(endpoint, Constants.RPC_METHOD, appletReqParams, new HashMap<>());
        log.info("sendAppletMsg res:"+result);
        JSONObject cmsJSONObject = JSONUtil.parseObj(result).getJSONObject("result");
        CmsInfoResDTO cmsInfoResDTO = JSONUtil.toBean(cmsJSONObject, CmsInfoResDTO.class);
        return cmsInfoResDTO;
    }

    /**
     * 微信小程序参数封装
     *
     * @param equName
     * @param equName
     * @param userId
     * @return
     */
    public JSONObject getAppletJSONParams(String homeName, String roomname, String equName, Integer userId, String title, String page, Date date) {
        JSONObject jsonObject = JSONUtil.createObj().set("method", Constants.APPLET_SEND_MSG);
        jsonObject.set(Constants.JSON_RPC, Constants.JSON_RPC_VERSION);
        jsonObject.set("id", UUID.randomUUID());
        JSONArray params = JSONUtil.createArray();
        Map<String, String> appletMap = new HashMap<>();
        if (StringUtils.isNotEmpty(roomname)) {
            homeName = homeName + " - " + roomname;
        }
        appletMap.put("date4", DateUtil.format(date, "yyyy年MM月dd日 HH:mm"));
        appletMap.put("thing5", homeName);
        appletMap.put("thing3", title);
        appletMap.put("thing1", equName);
        AppletInfoReqDTO appletInfoReqDTO = new AppletInfoReqDTO();
        appletInfoReqDTO.setUser_id(userId);
        appletInfoReqDTO.setTemplate_id(templateId);
        appletInfoReqDTO.setData(appletMap);
        appletInfoReqDTO.setPage(page);
        params.add(appletInfoReqDTO);
        jsonObject.set("params", params);
        log.info("appletInfoReqDTO：" + jsonObject);
        return jsonObject;
    }

    /**
     * 运行报告小程序参数封装
     *
     * @param homeName
     * @param title
     * @param userId
     * @return
     */
    public JSONObject getRunningReportParams(String homeName, Integer userId, String title, String date, String page) {
        JSONObject jsonObject = JSONUtil.createObj().set("method", Constants.APPLET_SEND_MSG);
        jsonObject.set(Constants.JSON_RPC, Constants.JSON_RPC_VERSION);
        jsonObject.set("id", UUID.randomUUID());
        JSONArray params = JSONUtil.createArray();
        Map<String, String> appletMap = new HashMap<>();
        appletMap.put("date4", date);
        appletMap.put("thing5", homeName);
        appletMap.put("thing2", title);
        AppletInfoReqDTO appletInfoReqDTO = new AppletInfoReqDTO();
        appletInfoReqDTO.setUser_id(userId);
        appletInfoReqDTO.setTemplate_id(runningTemplateId);
        appletInfoReqDTO.setData(appletMap);
        appletInfoReqDTO.setPage(page);
        params.add(appletInfoReqDTO);
        jsonObject.set("params", params);
        log.info("getRunningReportParams：" + jsonObject);
        return jsonObject;
    }
}